import axios from 'axios';
/*eslint-disable */
/**
 * 访客登录
 * @param {*} name
 * @param {*} pwd
 * @param {*} org
 */
export const login = (name, pwd, org) => {
	return axios.post(
		loginUrl + '/ivcs/api/v1/auth/logon',
		{
			userName: name,
			pwd: pwd,
			role: 'customer',
			org: org
		},
		{
			'Content-Type': 'application/json'
		}
	);
};

/**
 * 游客免密登录
 * @param {object} para body参数
 * @returns Promise.<object, Error>
 */
export const visitorLogin = (para) => {
	const crypto = require('crypto');
	const Accept = '*/*';
	const ContentType = 'application/json; charset=UTF-8';
	const time = new Date().toISOString();
	// const signatureHeaders = '';
	const stringToSign = 'POST' + '-' + Accept + '-' + ContentType.toLocaleUpperCase() + '-' + time + '-';
	console.log('testtt',stringToSign);
	const xCaSignature = crypto.createHash('sha224').update(stringToSign).digest('hex');
	return axios.post(loginUrl + '/ivcs/api/v1/auth/visitor', para, {
		headers: {
			accept: Accept,
			'content-type': ContentType,
			time: time,
			// 'x-ca-signature-headers': signatureHeaders,
			'x-ca-signature': xCaSignature,
		},
	});
};

/**
 * token失效，重新刷新链接
 * @param {*} para
 */
export const refreshToken = para => {
	return axios.post(loginUrl + '/ivcs/api/v1/auth/refresh-token', para, {
		headers: {
			'Content-Type': 'application/json'
		}
	});
};

/**
 * 访客注册
 * @param {*} name
 * @param {*} pwd
 * @param {*} org
 */
export const register = (name, pwd, org) => {
	return axios.post(
		loginUrl + '/ivcs/api/v1/auth/register',
		{
			userName: name,
			pwd: pwd,
			role: 'customer',
			org: org
		},
		{
			'Content-Type': 'application/json'
		}
	);
};

/**
 * 保存会话form信息
 * @param {Sting} sessionId 会话id
 * @param {Sting} formId 表单id
 *
 */
export const savesessionform = data => {
	return axios.post(loginUrl + `/ivcs/api/v1/sessions/form`, data, {
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};

/**
 * 访客提交form信息
 * @param {*} para
 */
export const postforminfo = para => {
	return axios.post(loginUrl + '/ivcs/api/v1/forminfo', para, {
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};

/**
 * 访客更新form信息
 * @param {*} formId
 * @param {*} para
 */
export const updateforminfo = (formId, para) => {
	return axios.put(loginUrl + '/ivcs/api/v1/forminfo/' + formId, para, {
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};

/**
 * 访客提交个人申请信息
 * @param {*} para
 */
export const postInputFormData = (sessionId, para) => {
	return axios.post(loginUrl + '/ivcs/api/v1/sessions/' + sessionId + '/applications', para, {
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};

/**
 * 保存图片base64
 * @param {*} para
 */
export const saveimg = para => {
	return axios.post(loginUrl + `/ivcs/api/v1/uploadImg`, para, {
		headers: {
			'Content-Type': 'application/json',
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};

/**
 * 获取当前机构所有分组
 */
export const getAllGroups = () => {
	return axios.get(loginUrl + '/ivcs/api/v1/groups/select/options', {
		headers: {
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};

/**
 * 获取当前机构下的所有业务场景
 */
export const getAllBizScenario = () => {
	// status = enabled 表示所有启用的业务场景  disabled 表示所有停用的业务场景
	return axios.get(loginUrl + '/ivcs/api/v1/bizscenarios/select/options?status=enabled', {
		headers: {
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};

/**
 * 上传文件
 * @param {*} data
 */
export const uploadfile = data => {
	return axios.post(loginUrl + '/ivcs/api/v1/upload', data, {
		headers: {
			'Content-Type': 'multipart/form-data',
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};

/**
 * 上传文件
 * @param {*} data
 */
export const uploadFileWithFormData = data => {
	return axios.post(loginUrl + '/ivcs/api/v1/upload', data, {
		headers: {
			'Content-Type': 'multipart/form-data',
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};

export const getFormDetails = id => {
	return axios.get(loginUrl + `/ivcs/api/v1/formTemplates/${id}/detail`, {
		headers: {
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};

// 创建访客会话邀请码
export const createInviteVisitorsCode = data => {
	return axios.post(loginUrl + `/ivcs/api/v1/customerInvitationLinks`, data, {
		headers: {
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};

/**
 * 获取通话水印和坐席ID配置项
 * @returns 
 */
export const getSystemConfig = () => {
	return axios.get(loginUrl + `/ivcs/api/v1/systemconfig/list`, {
		headers: {
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};


/**
 * 获取业务场景下的角色列表
 * @returns 
 */
export const getScenariosRoles = (scenId) => {
	return axios.get(loginUrl + `/ivcs/api/v1/bizscenarios/${scenId}/roles`, {
		headers: {
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};

/**
 * 获取业务场景下绑定的分组
 * @param {*} scenId 
 * @returns 
 */
export const getScenariosBindGroups = (scenId) => {
	return axios.get(loginUrl + `/ivcs/api/v1/bizscenarios/${scenId}/details`, {
		headers: {
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
}


/**
 * 判断是否是机器人坐席
 * @param {string} name 坐席名
 * @param {string} org 机构
 * @returns 
 */
export const getIsRobotAgent = (name,org) => {
	return axios.get(loginUrl + `/ivcs/api/v1/agents/org/${org}/name/${name}/isRobot`, {
		headers: {
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};


/**
 * 获取callcode
 * @param {*} data 
 * @returns 
 */
export const getCallCode = data => {
	return axios.post(loginUrl + '/ivcs/api/v1/invitationLinks', data, {
		headers: {
			'Content-Type': 'application/json',
        }
	});
}


/**
 * 获取所有标签
 * @returns 
 */
export const getBizLabels = () => {
	return axios.get(loginUrl + `/ivcs/api/v1/bizlabel/select/options`, {
		headers: {
			'x-access-token': localStorage.getItem('customer_token')
		}
	});
};
